"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 磁盘满时创建数据库
Create At   : 2021/01/08
@zou_jialiang0505328126
Description :
    1.使用CFE注入磁盘满故障
    2.创建数据库
    3.清除故障
    4.创建数据库
Expect      :
    1.注入故障成功
    2.创建数据库失败，提示空间不足
    3.清除故障成功
    4.创建数据库成功
History     :
    Modified by: @wan005 at 2021/9/27 优化用例，优化断言
    Modified by: @li-xin12345 at 2023/2/20 优化用例，修改数据库名
"""

import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Basebackup(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_Dbsys_Case232.py 开始执行------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.db_primary_root_node = Node(node='PrimaryRoot')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.DB_DISK_NAME = ''
        self.dbname = 'db_dbsystem_232'

    def test_basebackup(self):
        self.log.info('---------1.使用CFE注入磁盘满故障---------')
        self.log.info('====================获取主节点磁盘名=============')
        cmd = f"df -h {macro.DB_INSTANCE_PATH}"
        self.log.info(cmd)
        msg = self.db_primary_root_node.sh(cmd).result()
        self.log.info(msg)
        self.DB_DISK_NAME = msg.splitlines()[-1].split()[0].strip()

        cfedisk_cmd = f'{macro.CFE_PATH}/cfe  ' \
            f'"inject  rfile_full (diskname)  values  ({self.DB_DISK_NAME})"'
        result = self.db_primary_root_node.sh(cfedisk_cmd).result()
        self.log.info(result)

        self.log.info('-------------------2.创建数据库-----------------')
        result = self.commonshpri.executDbSql(
            f'create database {self.dbname};')
        self.log.info(result)
        self.assertTrue(self.Constant.DISK_FULL_MSG in result
                        or "failed to connect" in result)

        self.log.info('----------------------3.清除故障----------------------')
        cfedisk_cmd = f'''{macro.CFE_PATH}/cfe "clean  
        rfile_full  where(diskname={self.DB_DISK_NAME})"'''
        tmp = self.db_primary_root_node.sh(cfedisk_cmd).result()
        self.log.info(tmp)

        self.log.info('------重启数据库-----')
        result = self.commonshpri.stopDbCluster()
        self.log.info(result)
        is_started = self.commonshpri.startDbCluster()
        self.log.info(is_started)
        status = self.commonshpri.getDbClusterStatus("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)

        self.log.info('-------------------4.创建数据库-----------------')
        result = self.commonshpri.executDbSql(
            f'create database {self.dbname};')
        self.log.info(result)
        self.assertIn(self.Constant.CREATE_DATABASE_SUCCESS_MSG, result)

    def tearDown(self):
        self.log.info('---------------环境清理----------------')
        self.log.info('---------------清除故障----------------')
        cfedisk_cmd = f'''{macro.CFE_PATH}/cfe "clean  
        rfile_full  where(diskname={self.DB_DISK_NAME})"'''
        tmp = self.db_primary_root_node.sh(cfedisk_cmd).result()
        self.log.info(tmp)

        self.log.info('-------------------删除数据库-----------------')
        result = self.commonshpri.executDbSql(
            f'drop database if exists {self.dbname};')
        self.log.info(result)
        self.log.info('---Opengauss_Reliability_Dbsys_Case232.py 执行结束--')
